/*
Copyright (c) 2020 IanTang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "display.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
char Menu_Table[60] = { "Soldering Set DC Power Set"};
/* U8g2 */
u8g2_t u8g2;
/* U8x8 */
u8x8_t u8x8;

Menu_t menu;

/*
 * u8g2 constructor 
 * */
uint8_t u8x8_byte_hw_spi_futaba_vfd(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
  uint8_t *data;
  switch (msg)
  {
  case U8X8_MSG_BYTE_SEND:
    data = (uint8_t *)arg_ptr;
    for (uint8_t i = 0; i < arg_int; i++)
    {
      while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET)
      {
        __NOP();
      }
      LL_SPI_TransmitData8(SPI2, *data);
      data += 1;
    }
    while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET)
    {
      __NOP();
    }
    break;
  case U8X8_MSG_BYTE_START_TRANSFER://拉低CS
    VFD_CS_RESET();
    break;
  case U8X8_MSG_BYTE_END_TRANSFER://拉高CS
  
    LL_mDelay(1);
    VFD_CS_SET(); 
    break;
  default:
    return 0;
  }
  return 1;
}

uint8_t u8x8_gpio_and_delay_futaba_vfd(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
{
  switch (msg)
  {
  case U8X8_MSG_GPIO_AND_DELAY_INIT:
    LL_SPI_Enable(SPI2);
    break;
  case U8X8_MSG_DELAY_MILLI:
    LL_mDelay(arg_int);
    break;
  case U8X8_MSG_GPIO_RESET:
    if (arg_int)
      VFD_RST_SET();
    else
      VFD_RST_RESET();
    break;
  default:
    u8x8_SetGPIOResult(u8x8, 1);
    break;
  }
  return 1;
}

/**
 * @brief 初始化VFD显示所需的外设,设置构造函数
 * @rev 0
*/
uint8_t display_init( void )
{
  /* DC-DC Sync PWM */
  // LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH4);
  // LL_TIM_EnableAllOutputs(TIM1);
  // LL_TIM_EnableCounter(TIM1);
  // LL_TIM_GenerateEvent_UPDATE(TIM1);
  EN9016_EN();

  /* Panel LED PWM */
  // LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
  // LL_TIM_EnableAllOutputs(TIM2);
  // LL_TIM_EnableCounter(TIM2);
  // LL_TIM_GenerateEvent_UPDATE(TIM2);

    /* U8g2 */
  // u8g2_Setup_futaba_vfd_gp1287ai_256x50_1(&u8g2, U8G2_R0, u8x8_byte_hw_spi_futaba_vfd, u8x8_gpio_and_delay_futaba_vfd);
  // u8g2_Setup_futaba_vfd_gp1287ai_256x50_2(&u8g2, U8G2_R0, u8x8_byte_hw_spi_futaba_vfd, u8x8_gpio_and_delay_futaba_vfd);
  // u8g2_Setup_futaba_vfd_gp1287ai_256x50_f(&u8g2, U8G2_R0, u8x8_byte_hw_spi_futaba_vfd, u8x8_gpio_and_delay_futaba_vfd);

  // u8g2_Setup_futaba_vfd_rav4_253x63_1(&u8g2, U8G2_R0, u8x8_byte_hw_spi_futaba_vfd, u8x8_gpio_and_delay_futaba_vfd);
  u8g2_Setup_futaba_vfd_rav4_253x63_2(&u8g2, U8G2_R0, u8x8_byte_hw_spi_futaba_vfd, u8x8_gpio_and_delay_futaba_vfd);
  // u8g2_Setup_futaba_vfd_rav4_253x63_f(&u8g2, U8G2_R0, u8x8_byte_hw_spi_futaba_vfd, u8x8_gpio_and_delay_futaba_vfd);

  /* U8x8 */
  // u8x8_Setup(&u8x8, u8x8_d_futaba_vfd_gp1287ai_256x50, u8x8_cad_empty, u8x8_byte_hw_spi_futaba_vfd, u8x8_gpio_and_delay_futaba_vfd);
 
  u8x8_Setup(&u8x8,u8x8_d_futaba_vfd_rav4_253x63, u8x8_cad_empty, u8x8_byte_hw_spi_futaba_vfd,u8x8_gpio_and_delay_futaba_vfd);

  /* U8g2 */
  u8g2_InitDisplay(&u8g2);
  u8g2_SetPowerSave(&u8g2, RESET);
  u8g2_SetContrast(&u8g2, 255);
  /* U8x8 */
  u8x8_InitDisplay(&u8x8);
  u8x8_SetPowerSave(&u8x8, RESET);
  u8x8_SetContrast(&u8x8, 255);
  

  /* U8x8 Test -------------------------------------- */
  u8x8_SetFont(&u8x8, u8x8_font_chroma48medium8_r);
  u8x8_DrawString(&u8x8, 0, 0, "Hello World! T0");
  u8x8_DrawString(&u8x8, 0, 1, "Hello World! T1");
  u8x8_DrawString(&u8x8, 0, 2, "Hello World! T2");
  u8x8_DrawString(&u8x8, 0, 3, "Hello World! T3");
  LL_mDelay(3000);
    
  u8g2_SetContrast(&u8g2, 50);
  /* U8g2 Demo */
  GraphicsTest_PageBuffer(&u8g2);
  LL_mDelay(200);
  RotationTest_PageBuffer(&u8g2);
  LL_mDelay(200);

  u8g2_SetContrast(&u8g2, 200);
  
  /* U8g2 */
  u8g2_InitDisplay(&u8g2);
  u8g2_SetPowerSave(&u8g2, RESET);
  u8g2_SetContrast(&u8g2, 5);

  /* U8x8 */
  u8x8_InitDisplay(&u8x8);
  u8x8_SetPowerSave(&u8x8, RESET);
  u8x8_SetContrast(&u8x8, 5);
  return 1;
}

uint8_t display_test( void )
{
  
  /* U8x8 Test -------------------------------------- */
  u8x8_SetFont(&u8x8, u8x8_font_chroma48medium8_r);
  u8x8_DrawString(&u8x8, 0, 0, "Hello World! T0");
  u8x8_DrawString(&u8x8, 0, 1, "Hello World! T1");
  u8x8_DrawString(&u8x8, 0, 2, "Hello World! T2");
  u8x8_DrawString(&u8x8, 0, 3, "Hello World! T3");
  LL_mDelay(3000);
    
  u8g2_SetContrast(&u8g2, 50);
  /* U8g2 Demo */
  GraphicsTest_PageBuffer(&u8g2);
  LL_mDelay(200);
  RotationTest_PageBuffer(&u8g2);
  LL_mDelay(200);

  u8g2_SetContrast(&u8g2, 200);
  return 0;
}
/*
 * @brief:  re-flash monitor at a low frequence,also handle menu 
 * @para:   display data ,key value 
*/
uint8_t display_task(Display_t *display_data, Menu_t *menu, uint8_t key_value)
{
    uint8_t status =  menu->status;

    /* run state */
    if( menu->status == Display_Run)
    {
      /*display realtime data & info*/
      // u8x8_DrawString(&u8x8, 2, 0, display_data->Iron_Temp_Set);
      // u8x8_Draw2x2String(&u8x8, 2, 2, display_data->Iron_Temp);
      // u8g2_DrawHLine(&u8g2, 2, 5, display_data->Iron_Power);

      // u8x8_DrawString(&u8x8, 15, 0, display_data->Vol_Out_Set);
      // u8x8_DrawString(&u8x8, 25, 0, display_data->Cur_Out_Set);
      // u8x8_Draw2x2String(&u8x8, 15, 2, display_data->Vol_Out);
      // u8x8_Draw2x2String(&u8x8, 25, 2, display_data->Cur_Out);

    }
    else/* dispaly menu */
    {
      /* display menu title */
      if( menu->level == 1 )
      {
        u8x8_DrawString(&u8x8, 10, 2, "Set");
      }
      else
      {
        u8x8_DrawString(&u8x8, 10, 2, menu->table[menu->level]);
        // u8g2_DrawLine();
        // u8g2_DrawStr(&u8g2, );
        // u8g2_SetFont()
      }
    }
    LL_mDelay(50);
    return 1;
}